/**
 * Created by jackliu on 15/12/6.
 */

import SJGPage = require('./../kernel/cont/SJGPage');
import ObjManager = require('./../kernel/cont/ObjManager');
import TextObj = require('./../kernel/obj/TextObj');
import ImageObj = require('./../kernel/obj/ImageObj');
import GroupObj = require('./../kernel/obj/GroupObj');
import SJGReplaceGroup = require('./../kernel/replace/SJGReplaceGroup');
import SJGReplaceItemImage = require('./../kernel/replace/SJGReplaceItemImage');
import SJGReplaceItemText = require('./../kernel/replace/SJGReplaceItemText');
import SJGSubGroupImageTextOld = require('./../kernel/replace/SJGSubGroupImageTextOld');
import SJGTypeSetItemBase = require('./SJGTypeSetItemBase');
import SJGTypeSetBoxUtil2 = require('./SJGTypeSetBoxUtil2');
import ObjBase = require('./../kernel/obj/ObjBase');
import CommonUtil = require('./../../util/CommonUtil');
import ObjPositionAlign = require('./../kernel/base/ObjPositionAlign');
import GCJ_CLASS_TYPE= require('./../common/GCJ_CLASS_TYPE');
import ParaContour = require('./../common/graph/ParaContour');
var async  = require( 'async'); // 同步控制
export = SJGTypeSetImageTextGroup;

class SJGTypeSetImageTextGroup extends  SJGTypeSetItemBase{

    // 根据坐标和宽高排版对象，需要被重载
    // 参数：p页面
    // 参数：group标题组
    // 参数：i可替换对象的下标
    // 参数：pos对象的宽高
    public typesetObject(p:SJGPage,group: SJGReplaceGroup,i:number, pos:ObjPositionAlign){
        var objMgr:ObjManager = p.getGlobalData().objManager();
        var groupObj:GroupObj = <GroupObj>objMgr.getObject(group.id);// 原版面中的对象
        var replaceItemImageText:SJGSubGroupImageTextOld = <SJGSubGroupImageTextOld>group.value[i];
        var G:GroupObj = null;

        if(i<groupObj.children.length){
            // 修改旧对象GroupObj G以及其文字子对象TextObj T 和图片子对象ImageObj I
            G =<GroupObj> groupObj.children[i];
        }else{
            // 创建新对象,深拷贝最后一个子GroupObj
            // 得到GroupObj G以及其文字子对象TextObj T 和图片子对象ImageObj I
            G =<GroupObj> CommonUtil.deepCopy(groupObj.children[groupObj.children.length-1],p.getGlobalData().ver,p.getGlobalData());
        }
        var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj);
        var T:TextObj =<TextObj> objMgr.getObject(arr[0]);
        if(!T)  return;

        var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_ImageObj);
        var I:ImageObj =<ImageObj> objMgr.getObject(arr[0]);
        if(!I)  return;

        // G的位置尺寸设置为pos
        //G.pos.copy(pos);


        // 设置T的te和pc
        T.te = replaceItemImageText.text.value;
        T.pc = ParaContour.fromFontServerData(replaceItemImageText.text.value, JSON.parse(replaceItemImageText.text.originContour));
        // 重新排版文字
        T.refresh();

        //todo 以下需要根据扩展方式进行修正
        // 设置T的w、h、source
        //I.pos.w = pos.w;
        //I.pos.h = pos.h;

        if(replaceItemImageText.image.value){
            I.source = replaceItemImageText.image.value;
        }
        // 调整T的pos适配到T中
        I.image = I.pos.adjust(replaceItemImageText.image.pos.w, replaceItemImageText.image.pos.h);
        console.log("ffffff image :"+I.image.w+","+I.image.h);


        // 对于圆形扩展，不进行缩放，移动位置即可
        if(group.advance.extend_method == SJGReplaceGroup.CIRCLE){
            G.align = pos;
            var ap = pos.toPos();
            G.pos.copy(ap);

            return;
        }

        // 调整编组G和内部子对象的大小
        //var arr:Array<number> = G.getChildOfTypes([GCJ_CLASS_TYPE.CLASS_TYPE_TextObj,GCJ_CLASS_TYPE.CLASS_TYPE_ImageObj],true);
        //for(var k in arr){
        //    var other:ObjBase =  objMgr.getObject(arr[k]);
        //    other.constrain.scale = 0; // 将可变对象之外的所有对象约束设置为固定
        //}

        var ret:Array<number> = SJGTypeSetBoxUtil2.calcRatioXY(groupObj.children.length, group.value.length,
            group.advance.extend_method,group.advance.limit);
        G.expand(1, ret[0],  1,      false); // 向右扩展
        G.expand(2, 1,       ret[1], false);// 向下扩展

    }

    // 排版后到调整：例如图文组需要增减字号达到填满效果
    // 参数：p页面
    // 参数：group标题组
    // 参数：callback 为async的回调函数，例如：asyncCallback(null, 'one');
    public after(p:SJGPage,group: SJGReplaceGroup ,callback) {
        var objMgr:ObjManager = p.getGlobalData().objManager();
        var groupObj:GroupObj = <GroupObj>objMgr.getObject(group.id);// 原版面中的对象
        var G:GroupObj = null;

        // 获取最大字数的文字对象、最大字数、到父Group的距离
        var maxTextToBottom:number = 0;
        var maxTextLen:number= 0;
        var maxText:TextObj=null;
        for(var i in groupObj.children){
            G =<GroupObj> groupObj.children[i];
            if(!G)  return;


            var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj);
            var T:TextObj =<TextObj> objMgr.getObject(arr[0]);
            if(!T)  return;

            if(T.te.length>maxTextLen){
                maxTextToBottom = T.pos.h;
                maxText = T;
                maxTextLen = T.te.length;
            }
        }

        // 缩放字号
        if(maxTextLen>0 && maxTextToBottom>0 && maxText.te.length>0){
            var rw:number = Math.sqrt((maxTextToBottom*maxText.pos.w)/maxTextLen);
            var ratio:number = (rw-maxText.cs-maxText.ls)/maxText.fs;
            console.log("======>ImageTextGroup ratio is: ",maxTextToBottom," ", ratio)
            if(ratio>0){
                for(var i in groupObj.children){
                    G =<GroupObj> groupObj.children[i];
                    if(!G)  return;

                    var arr:Array<number> = G.getChildOfType(GCJ_CLASS_TYPE.CLASS_TYPE_TextObj);
                    var T:TextObj =<TextObj> objMgr.getObject(arr[0]);
                    if(!T)  return;
                    // 缩放字号、字间距、行间距
                    T.fs *= ratio;
                    T.cs *= ratio;
                    T.ls *= ratio;
                    // 缩放每个字的轮廓
                    T.pc.adjustPosition(ratio,ratio,2);

                    // 重新排版
                    T.refresh();
                }
            }
        }

        // 回调
        callback(null, null);
    }
}
